package com.zmj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zmj.dto.TableData;
import com.zmj.entity.ScheduleJob;
import com.zmj.mapper.ScheduleJobMapper;
import com.zmj.utils.Response;
import com.zmj.utils.ScheduleUtils;
import org.quartz.Scheduler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ScheduleService {
    @Resource
    private ScheduleJobMapper scheduleJobMapper;

    @Resource
    private Scheduler scheduler;//applicationContext-quartz.xml


    public TableData findScheduleJob(String search, String order, int limit, int offset) {
        PageHelper.offsetPage(offset, limit);

        List<ScheduleJob> list = scheduleJobMapper.findScheduleJOb(search, order);

        PageInfo info = new PageInfo(list);

        TableData tableData = new TableData();
        tableData.setRows(info.getList());
        tableData.setTotal(info.getTotal());
        return tableData;
    }


    public Response insertScheduleJob(ScheduleJob scheduleJob) {
        //数据库操作
        int i = scheduleJobMapper.insertSelective(scheduleJob);
        //新增scheduleJob? 返回自增主键id?
        System.out.println("--->自增主键id" + scheduleJob.getJobId());

        //创建定时任务

        ScheduleUtils.createTask(scheduler, scheduleJob);//(Scheduler scheduler, ScheduleJob scheduleJob) {


        return i > 0 ? Response.success() : Response.error();
    }


    public Response updateScheduleJob(ScheduleJob scheduleJob) {
        //数据库操作
        int i = scheduleJobMapper.updateByPrimaryKeySelective(scheduleJob);

        //修改定时任务
        ScheduleUtils.updateScheduler(scheduler, scheduleJob);

        return i > 0 ? Response.success() : Response.error();
    }


    public Response findScheduleJobById(long scheduleJobId) {
        ScheduleJob scheduleJob = scheduleJobMapper.selectByPrimaryKey(scheduleJobId);
        return Response.success().put("scheduleJob", scheduleJob);
    }


    public Response deleteScheduleJob(long scheduleJobId) {
        int i = scheduleJobMapper.deleteByPrimaryKey(scheduleJobId);

        //删除定时任务
        ScheduleUtils.deleteScheduler(scheduler, scheduleJobId);
        return i > 0 ? Response.success() : Response.error();
    }


    public Response deleteByIds(List<Long> ids) {
        int i = scheduleJobMapper.deleteByIds(ids);

        //删除定时任务
        for (Long id : ids) {
            ScheduleUtils.deleteScheduler(scheduler, id);
        }

        return i > 0 ? Response.success() : Response.error();
    }


    public Response resumeTask(List<Long> ids) {
        //修改scheduleJob状态
        int i = scheduleJobMapper.updateStatus(ids, (byte) 0);
        //恢复任务
        for (Long id : ids) {
            ScheduleUtils.resume(scheduler, id);
        }

        return i > 0 ? Response.success() : Response.error();
    }


    public Response pauseTask(List<Long> ids) {
        //修改scheduleJob状态
        int i = scheduleJobMapper.updateStatus(ids, (byte) 1);
        //恢复任务
        for (Long id : ids) {
            ScheduleUtils.pause(scheduler, id);
        }

        return i > 0 ? Response.success() : Response.error();
    }


    public Response runOnceTask(List<Long> ids) {
        //恢复任务
        for (Long id : ids) {
            ScheduleUtils.runOnce(scheduler, id);
        }

        return Response.success();
    }
}
